//
//  UIButton+QTZ.m
//  QTZ
//
//  Created by ski on 2017/8/1.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "UIButton+QTZ.h"
#import <objc/runtime.h>

static char *NormalColorKey = "NormalColor";
static char *HighlightColorKey = "HighlightColor";

@interface UIButton ()

@property (nonatomic, strong) UIColor *normalColor;
@property (nonatomic, strong) UIColor *highlightColor;

@end

@implementation UIButton (QTZ)

+ (UIButton *)blackBackBtnWithTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    return [self leftBarButtonWithImage:[UIImage imageNamed:@"icon_nav_back_black"] highlightedImage:[UIImage imageNamed:@"icon_nav_back_black"] target:target action:action forControlEvents:controlEvents];
}

+ (UIButton *)whiteBackBtnWithTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    return [self whiteBarButtonWithImage:[UIImage imageNamed:@"icon_nav_back_white"] highlightedImage:[UIImage imageNamed:@"icon_nav_back_white"] target:target action:action forControlEvents:controlEvents];
}

+ (UIButton *)leftBarButtonWithImage:(UIImage*)image highlightedImage:(UIImage*)highlightedImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    barButton.frame = CGRectMake(0.0f, 0.0f, 44, 44);
//    barButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//    [barButton setTitle:@"返回" forState:UIControlStateNormal];
//    barButton.titleLabel.font = FONT(14);
//    [barButton setTitleColor:QTZ_Gray73 forState:UIControlStateNormal];
    [barButton setImage:image forState:UIControlStateNormal];
//    [barButton setImage:highlightedImage forState:UIControlStateHighlighted];
//    barButton.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
    [barButton addTarget:target action:action forControlEvents:controlEvents];
    [barButton sizeToFit];
    [barButton setExclusiveTouch:YES];
    return barButton;
}

+ (UIButton *)whiteBarButtonWithImage:(UIImage*)image highlightedImage:(UIImage*)highlightedImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    barButton.frame = CGRectMake(0.0f, 0.0f, 44, 44);
    //    barButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//    [barButton setTitle:@"返回" forState:UIControlStateNormal];
//    barButton.titleLabel.font = FONT(14);
//    [barButton setTitleColor:QTZ_White forState:UIControlStateNormal];
    [barButton setImage:image forState:UIControlStateNormal];
    //    [barButton setImage:highlightedImage forState:UIControlStateHighlighted];
//    barButton.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
    [barButton addTarget:target action:action forControlEvents:controlEvents];
    [barButton sizeToFit];
    [barButton setExclusiveTouch:YES];
    return barButton;
}

- (void)setButtonImageAndTitleWithSpace:(CGFloat)spacing WithButton:(UIButton *)btn
{
    CGSize imageSize = btn.imageView.frame.size;
    CGSize titleSize = btn.titleLabel.frame.size;
    CGSize textSize = [btn.titleLabel.text sizeWithUIFont:btn.titleLabel.font forWidth:btn.titleLabel.width];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width > imageSize.width ? textSize.width : imageSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    btn.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
    btn.width = frameSize.width;
    btn.height = totalHeight;
}

#pragma mark --
- (void)setNormalBackgroundColor:(UIColor *)normalColor highlightBackgroundColor:(UIColor *)highlightColor
{
    self.normalColor = normalColor;
    self.highlightColor = highlightColor;
    [self setBackgroundColor:self.normalColor];
    [self addTarget:self action:@selector(changeTouchDownColor) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(resetColor) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setNormalColor:(UIColor *)normalColor
{
    objc_setAssociatedObject(self, NormalColorKey, normalColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIColor *)normalColor
{
    return objc_getAssociatedObject(self, NormalColorKey);
}

- (void)setHighlightColor:(UIColor *)highlightColor
{
    objc_setAssociatedObject(self, HighlightColorKey, highlightColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIColor *)highlightColor
{
    return objc_getAssociatedObject(self, HighlightColorKey);
}

- (void)changeTouchDownColor
{
    [self setBackgroundColor:self.highlightColor];
}

- (void)resetColor
{
    [self setBackgroundColor:self.normalColor];
}
#pragma mark --



@end
